/**
 * Created by Administrator on 2018/11/30.
 */
$CertusJsonAjax.ajax("borrowByIdPrint",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){
    $$scope.borrow = {}
    $$scope.borrow.personVisible = true;
    //personal select
    $$scope.personalSelect = [
        {name:"个人",code:"personal"},
        {name:"项目",code:"project"}
    ]


    $$scope.wkStepName = $CertusJsonAjax.getWkStepName();

    $$scope.borrowBill = res.data.borrow;

    $$scope.baseInfo = res.data.baseUser;

    $$scope.departmentName = res.data.departmentName;

    $$scope.compCode = res.data.compCode;

    // $$scope.compName = res.data

    tagName($$scope.compCode);

    if($$scope.borrowingType == "personal"){
        $$scope.borrowBill.projName = "个人";
        $$scope.borrowBill.borrowingName = "个人";
    }else{
        $$scope.borrowBill.borrowingName = "项目";
    }

    $$scope.processs = res.data.borrow.processs;

    $$scope.rmb = upDigit($$scope.borrowBill.borrowingAmount);


    if($$scope.processs.length == 0){
        $$scope.applog = false;
        $$scope.apploged = true;
        return $$scope.applog;
    }else{
        $$scope.applog = true;
        $$scope.apploged = false;
        return $$scope.applog;
    }



})


$$scope.borrowApprovalPrint = function(){
    layer.confirm('已递交打印的纸质单据？', {
        btn: ['已递交', '未递交'] //按钮
    }, function () {
        $CertusJsonAjax.ajax("borrowApprovalToEnd",{
            "processInstanceId": $CertusJsonAjax.getProInstanceId(),
            "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
        },function(res){
            if(res.data == "success"){
                layer.msg('转入财务审核', {icon: 1});
                findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                history.back();
            }
        })
    });
}

function tagName(compName){

    $$scope.compName = "";
    if(compName == "STSJT"){
        $$scope.compName = "南京";
    }
    if(compName == "STSNJ"){
        $$scope.compName = "南京";
    }
    if(compName == "STSBJ"){
        $$scope.compName = "北京";
    }
    if(compName == "STSGD"){
        $$scope.compName = "广东";
    }
    if(compName == "STSSH"){
        $$scope.compName = "上海";
    }
    return $$scope.compName;
};

// RMB大写转换
function upDigit(n) {
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);

    var s = '';

    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);

    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}

$$scope.printBorrowBill = function (cc) {
    event.preventDefault();
    html2canvas(document.getElementById("containerPrint-1"), {
        allowTaint: true,
        taintTest: false,
        onrendered: function (canvas) {
            canvas.id = "mycanvas";
            //生成base64图片数据
            var dataUrl = canvas.toDataURL();
            var newImg = document.createElement("img");
            newImg.id = "printImg-1";
            newImg.src = dataUrl;
            $('.printImg-1').show();
            document.getElementById('printImgBox-1').appendChild(newImg);
            $('#printImg-1').jqprint({
                debug: false,
                importCSS: true,
                printContainer: true,
                operaSupport: false
            });
        }
    })
}